import modelExtend from 'dva-model-extend'
import { parse } from 'qs'
import { pageModel } from './common'
import { organ,group } from 'services/newfile'
import { totalNum,totalList } from 'services/statistics'
import { infoDetais } from 'services/search'
import { changeState,remove,userLists,sends } from 'services/search'

export default modelExtend(pageModel, {
	  namespace: 'statistics',
	  state: {
	  	showDetails:false,
	  	list:[],
	  	listDetais:[],
	  	visible:false,
	  	showInfo:false,
	  	department1:'',
	  	params:'',
	  	details:'',
	  	department2:'',
	  	depUser:'',
	  	infoData:'',
	  	pagination2: {
	      showSizeChanger: true,
	      showQuickJumper: true,
	      showTotal: total => `共 ${total} 条`,
	      current: 1,
	      total: null
	    },
	  },
	  subscriptions: {
	    setup({dispatch,history}){
    		history.listen((location)=>{
    			if(location.pathname === '/total/statistics'){
	    				dispatch({
	    					type:'groups',
	    					payload:location.query,
	    				})
    				}
    			})
    		}
	  },
	  effects:{
	   	*groups({payload},{call,put}){
	   		const data = yield call(organ, payload)
	   		if(data.success){
	   			yield put({
	   				type:"querySuccess3",
	   				payload:{
	   					department1:data.list,
	   				}
	   			})
	   		}
	   	},	  
	   	*dep({payload},{call,put}){
	   		const data = yield call(group,payload)
	   		if(data.success){
	   			yield put({
	   				type:"querySuccessA",
	   				payload:{
	   					department2:data.list
	   				}
	   			})
	   		}
	   	},
	   	*users({payload},{call,put}){
	   		const data = yield call(userLists,payload)
	   		if(data){
	   			yield put({
	   				type:"querySuccessB",
	   				payload:{
	   					depUser:data.userList
	   				}
	   			})
	   		}
	   	},
	  	*info({payload},{call,put}){
			const data = yield call (infoDetais, payload)	
			yield put({type: 'showModal'});
			yield put({ 
	    			type: 'querySuccessC',
	    			payload: {
		          infoData:data,
		        }
	    		})
	  	},	  	
	  	*showInfos({payload},{call,put}){
	  		const data = yield call (infoDetais, payload)
	  		yield put({ 
	    			type: 'querySuccess',
	    			payload: {
		          showInfo:true,
		          showDetails:false,
		          details:data,
		        }
	    		})
	  	},
	   	*showDet({payload},{call,put}){
	    		yield put({ 
	    			type: 'querySuccess',
	    			payload: {
		          showDetails:true,
		          showInfo:false,
		        }
	    		})
	    },	  	
	   	*showLis({payload},{call,put}){
	    		yield put({ 
	    			type: 'querySuccess',
	    			payload: {
		          showDetails:false,
		          showInfo:false,
		        }
	    		})
	    }, 	 
	    *group({payload},{call,put}){
	    		const data = yield call(totalNum, payload)
//	    		console.log(data)
	   		if(data.success){
	   			yield put({
	   				type:"querySuccess1",
	   				payload:{
	   					list:data.documentCountList
	   				}
	   			})
	   		}
	    },
	    *details({payload},{call,put}){
	    		const data = yield call(totalList,payload)
//	    		console.log(data)
	    		yield put({
	    			type:'querySuccess2',
	    			payload:{
	    				params:payload,
	    				listDetais:data.page.list,
	    				pagination2: {
			            total: data.page.totalCount,
			            current: data.page.currPage
			          }
	    			}
	    		})
	    		yield put({ type: 'showDet' });
	    },
	  	*'delete' ({ payload }, { call, put }) {
	//	      console.log("111")
		      yield put({ type: 'showLoading' })
		      const data = yield call(remove, { number: payload })
		      if (data.success) {
		        yield put({ type: 'reload' });
		      }else{
		        yield put({ type: 'hideLoading' })
		      }
	    },
	    *complete({ payload },{ call, put }){
	    		  yield put({ type: 'showLoading' })
		      const data = yield call(changeState, payload )
		      if (data.success) {
		        yield put({ type: 'reload' });
		      }else{
		        yield put({ type: 'hideLoading' })
		      }
	    },    
	    *reload(action, { put, select }) {
	      const page = yield select(({ statistics }) => statistics.pagination2)
	
	      yield put({ type: 'details', payload: { 
	          current:page.current
	       } });
	    },
	  	*update({payload},{call,put}){
		      yield put({ type: 'hideModal' })
		      yield put({ type: 'showLoading' })
	//	      const id = yield select(({ contract }) => contract.currentItem.id)
	//	      const newContract = { ...payload, id }
		      const data = yield call(sends, payload)
		      if (data.success) {
		        yield put({ type: 'reload' });
		      }else{
		        yield put({ type: 'hideLoading' })
		      }  		
	  },	    
	  },
	  reducers:{
	  	showLoading (state) {
	      return { ...state, loading: true }
	    },
	    hideLoading (state) {
	      return { ...state, loading: false }
	    },	  	
	    querySuccessA (state, action) {
	      const {department2} = action.payload
	      return { ...state,
	      	department2
	      }
	    },   
	    querySuccessB (state, action) {
	      const {depUser} = action.payload
	      return { ...state,
	      	depUser
	      }
	    },  
	    querySuccessC (state, action) {
	      const {infoData} = action.payload
	      return { ...state,
	      	infoData
	      }
    		}, 
	    querySuccess(state, action){
	    		const {showDetails,showInfo,details} = action.payload
		      return { ...state,
		        showDetails,
		        showInfo,
		        details,
		      }
	    },	  
	    querySuccess1(state, action){
	    		const {list} = action.payload
		      return { ...state,
		        list,
		      }
	    },	
	    querySuccess2(state, action){
	    		const {listDetais,params,pagination2} = action.payload
		      return { ...state,
		        listDetais,
		        pagination2,
		        ...pagination2,
		        params
		      }
	    },
	    querySuccess3 (state, action) {
	      const {department1} = action.payload
	      return { ...state,
	      	department1
	      }
	    },
	    showModal(state, action){
	    		return {
	    			...state,
	    			...action.payload,
	    			visible:true,
	    		}
	    },
	    hideModal (state, action){
		      return { ...state, 
		        ...action.payload, 
		        visible: false 
		      }
	    },
	    onCancel (state, action) {
	      return { ...state,
	        visible: false,
	      }
	    },	    
	  }
})
